<?php
class hotPost extends WP_Widget {
	private $title = '热门文章';
	private $showposts = 5;

	function __construct() {
		parent::__construct('hot_post', $this->title, ['description' => '输出阅读最多的文章']);
	}

	public function form($instance) {
		$instance = wp_parse_args($instance, array(
			'title' => $this->title,
			'showposts' => $this->showposts,
			'datequery' => '',
		));
		$options = array(
			'360' => '一年',
			'90' => '季度',
			'30' => '一月',
			'7' => '一周',
		);
		$options_html = '<option value="0">所有</option>';
		foreach($options as $key=>$val) {
			$selected = $instance['datequery'] == $key ? ' selected' : '';
			$options_html .= "\n<option value=\"{$key}\"{$selected}>{$val}</option>";
		}
		$output = '<p><label for="widget-hot-post-title">标题：</label>
				<input class="widefat" id="widget-hot-post-title" name="' . $this->get_field_name('title') . '" type="text" value="' . $instance['title'] . '">
			</p>
			<p><label for="widget-hot-post-showposts">列表数量：</label>
				<input class="tiny-text" id="widget-hot-post-showposts" name="' . $this->get_field_name('showposts') . '" type="number" step="1" min="1" value="' . $instance['showposts'] . '">
			</p>
			<p><label for="widget-hot-post-datequery">时间范围：</label>
				<select id="widget-hot-post-datequery" name="' . $this->get_field_name('datequery') . '">'
					. $options_html . 
				'</select>
			</p>';
		echo $output;
	}

	public function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = sanitize_text_field($new_instance['title']);
		$instance['showposts'] = (int) $new_instance['showposts'];
		$instance['datequery'] = (int) $new_instance['datequery'];
		return $instance;
	}

	public function widget($args, $instance) {
        extract($args);
		$instance['datequery'] = isset($instance['datequery']) ? $instance['datequery'] : 0;
		echo $before_widget . $before_title . $instance['title'] . $after_title;
		echo '<ul>' . bzg_get_hot_posts($instance['showposts'], $instance['datequery']) . '</ul>';
		echo $after_widget;
	}

}
class likePost extends WP_Widget {
	private $title = '最受欢迎';
	private $showposts = 5;

	function __construct() {
		parent::__construct('like_post', $this->title, ['description' => '输出点赞最多的文章']);
	}

	public function form($instance) {
		$instance = wp_parse_args($instance, array(
			'title' => $this->title,
			'showposts' => $this->showposts,
			'datequery' => '',
		));
		$options = array(
			'360' => '一年',
			'90' => '季度',
			'30' => '一月',
			'7' => '一周',
		);
		$options_html = '<option value="0">所有</option>';
		foreach($options as $key=>$val) {
			$selected = $instance['datequery'] == $key ? ' selected' : '';
			$options_html .= "\n<option value=\"{$key}\"{$selected}>{$val}</option>";
		}
		$output = '<p><label for="widget-like-post-title">标题：</label>
				<input class="widefat" id="widget-like-post-title" name="' . $this->get_field_name('title') . '" type="text" value="' . $instance['title'] . '">
			</p>
			<p><label for="widget-like-post-showposts">列表数量：</label>
				<input class="tiny-text" id="widget-like-post-showposts" name="' . $this->get_field_name('showposts') . '" type="number" step="1" min="1" value="' . $instance['showposts'] . '">
			</p>
			<p><label for="widget-like-post-datequery">时间范围：</label>
				<select id="widget-like-post-datequery" name="' . $this->get_field_name('datequery') . '">'
					. $options_html . 
				'</select>
			</p>';
		echo $output;
	}

	public function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = sanitize_text_field($new_instance['title']);
		$instance['showposts'] = (int) $new_instance['showposts'];
		$instance['datequery'] = (int) $new_instance['datequery'];
		return $instance;
	}

	public function widget($args, $instance) {
        extract($args);
		$instance['datequery'] = isset($instance['datequery']) ? $instance['datequery'] : 0;
		echo $before_widget . $before_title . $instance['title'] . $after_title;
		echo '<ul>' . bzg_get_like_posts($instance['showposts'], $instance['datequery']) . '</ul>';
		echo $after_widget;
	}

}

function bzg_register_widget() {
	register_widget('hotPost');
	register_widget('likePost');

	register_sidebar(array(
		'name' => '边栏',
		'id' => 'sidebar-custom',
		'description' => '',
		'class' => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));

	register_sidebar(array(
		'name' => '文章内容前',
		'id' => 'content-before',
		'description' => '',
		'class' => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));
	register_sidebar(array(
		'name' => '文章内容后',
		'id' => 'content-after',
		'description' => '',
		'class' => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));
}
add_action('widgets_init', 'bzg_register_widget');
?>
